package top.smallway.UI;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class MyPanels extends JPanel {
    private Image backgroundImage;

    @Override
    public void setSize(int width, int height) {
        super.setSize(getWidth(), getHeight());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // 调用父类的paintComponent方法
        // 绘制背景图片
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
        }
    }

    public MyPanels(String background_img) {
        try {
            // 读取图片文件
            backgroundImage = ImageIO.read(new File(background_img));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
